﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace DentalSoftware
{
    public partial class SoDoRangForm : Form
    {
        private BenhNhanDTO benhnhan;
        private HinhAnhRang[,] dshar;

        public SoDoRangForm(BenhNhanDTO bn)
        {
            InitializeComponent();
            benhnhan = bn;
            dshar = new HinhAnhRang[4, 8];
        }

        private void SoDoRangForm_Load(object sender, EventArgs e)
        {
            InitDSHar();
            LoadSoDoRang();
            txtTenBenhNhan.Text = benhnhan.Hoten;
        }

        private void InitDSHar()
        {
            foreach (Control ctrl in Controls)
            {
                if (ctrl.Name.Substring(0, 3) == "har")
                {
                    HinhAnhRang temp = (HinhAnhRang)ctrl;
                    temp.Rang.Rang = temp.Name.Substring(3, 2);
                    int vung, vitri;
                    vung = int.Parse(temp.Name.Substring(3, 1)) - 1;
                    vitri = int.Parse(temp.Name.Substring(4, 1)) - 1;
                    dshar[vung, vitri] = temp;
                }
            }
            int t = 0;
        }

        private void LoadSoDoRang()
        {
            List<RangDTO> dsrang = new List<RangDTO>();
            dsrang = BenhNhanBUS.SelectAllTTRByMaBN(benhnhan.Ma);
            for (int i = 0; i < dsrang.Count; i++)
            {
                int t1 = int.Parse(dsrang[i].Rang.Substring(0, 1));
                int t2 = int.Parse(dsrang[i].Rang.Substring(1, 2));
                dshar[t1, t2].SetTinhTrangRang(dsrang[i].MatRang, dsrang[i].TinhTrang);
            }
        }

        private void LuuSoDoRang()
        {
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 8; j++)
                {
                    HinhAnhRang temp = dshar[i, j];
                    for (int t = 0; t < 5; t++)
                        if (temp.Rang.TinhTrang[t] != 0)
                        {
                            LuuTinhTrangRang(i.ToString() + j.ToString(), t, temp.Rang.TinhTrang[t], "");
                        }
                }

        }
        private void LuuTinhTrangRang(string rang, int mat, int tinhtrang, string thongtin)
        {
            BenhNhanBUS.InsertUpdateBenhNhan_Rang(benhnhan.Ma, rang, mat, tinhtrang, thongtin);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            LuuSoDoRang();
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void HAR_MouseDown(object sender, MouseEventArgs e)
        {
            HinhAnhRang temp = (HinhAnhRang)sender;
            int vt;
            if (e.Button == MouseButtons.Left)
            {
                vt = temp.ThayDoiTrangThai(e.Location);

            }
            if (e.Button == MouseButtons.Right)
            {
                Point t = new Point(this.Location.X + temp.Location.X + e.X + 5, this.Location.Y + temp.Location.Y + e.Y + 25);
                contextMenuStrip1.Show(t);
            }
        }

        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
    }
}
